package com.example.shixunqq.util;

import android.graphics.Color;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class ChartUtil {
    
    public static void setupHourlyChart(LineChart chart, List<Float> temperatures, List<String> times) {
        // 创建数据点
        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < temperatures.size(); i++) {
            entries.add(new Entry(i, temperatures.get(i)));
        }

        // 创建数据集
        LineDataSet dataSet = new LineDataSet(entries, "温度");
        styleDataSet(dataSet);

        // 设置数据
        LineData lineData = new LineData(dataSet);
        chart.setData(lineData);

        // 设置X轴
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                int index = (int) value;
                if (index >= 0 && index < times.size()) {
                    return times.get(index);
                }
                return "";
            }
        });

        styleChart(chart);
        chart.invalidate();
    }

    public static void setupDailyChart(LineChart chart, List<Float> maxTemps, List<Float> minTemps, List<String> dates) {
        List<Entry> maxEntries = new ArrayList<>();
        List<Entry> minEntries = new ArrayList<>();
        
        for (int i = 0; i < maxTemps.size(); i++) {
            maxEntries.add(new Entry(i, maxTemps.get(i)));
            minEntries.add(new Entry(i, minTemps.get(i)));
        }

        // 最高温度数据集
        LineDataSet maxDataSet = new LineDataSet(maxEntries, "最高温度");
        styleDataSet(maxDataSet);
        maxDataSet.setColor(Color.RED);

        // 最低温度数据集
        LineDataSet minDataSet = new LineDataSet(minEntries, "最低温度");
        styleDataSet(minDataSet);
        minDataSet.setColor(Color.BLUE);

        // 设置数据
        LineData lineData = new LineData(maxDataSet, minDataSet);
        chart.setData(lineData);

        // 设置X轴
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                int index = (int) value;
                if (index >= 0 && index < dates.size()) {
                    return dates.get(index);
                }
                return "";
            }
        });

        styleChart(chart);
        chart.invalidate();
    }

    private static void styleDataSet(LineDataSet dataSet) {
        dataSet.setDrawCircles(true);
        dataSet.setCircleRadius(4f);
        dataSet.setDrawValues(true);
        dataSet.setValueTextSize(9f);
        dataSet.setLineWidth(2f);
        dataSet.setMode(LineDataSet.Mode.LINEAR);
    }

    private static void styleChart(LineChart chart) {
        // 基本设置
        chart.getDescription().setEnabled(false);
        chart.setTouchEnabled(true);
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);
        chart.setPinchZoom(true);
        chart.setDrawGridBackground(false);

        // Y轴设置
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setDrawGridLines(true);
        chart.getAxisRight().setEnabled(false);

        // 图例设置
        chart.getLegend().setEnabled(true);

        // 动画
        chart.animateX(1000);
    }
} 